package xyz.masaikk.t1;

class Solution {
    public int[] searchRange(int[] nums, int target) {
        int [] result = new int[2] ;
        result[0]=-1;
        result[1]=-1;
        int [] ErrResult = new int[2] ;
        ErrResult[0]=-1;
        ErrResult[1]=-1;
        int [] ZeroResult = new int[2] ;
        ZeroResult[0]=0;
        ZeroResult[1]=0;
        if(nums.length<=0)
            return ErrResult;
        else {
            if(nums.length!=1){
                int staticLength=nums.length;
                if(nums[0]>target||nums[staticLength-1]<target){
                    return ErrResult;
                    //out of index
                }else {
                    result[0]=getFirst(nums,target);
                    if(result[0]==-1){
                        return ErrResult;
                    }else {
//                    result[1]=result[0];
                        result[1]=getTermial(nums,target,result[0]);
                        return result;

                    }

                }
            }
            else {
                if(nums[0]!=target){
                    return ErrResult;
                }else {
                    return ZeroResult;
                }
            }


        }

//        return ErrResult;
    }

    public int getFirst(int[] nums, int target){
        int index0=0;
        if(nums[index0]==target){
            return 0;
        }else {
            while(nums[index0]<target){
                index0+=1;
            };
            if(nums[index0]>target){
                //out of index1
                return -1;
            }else {
                return index0;
            }
        }
    }
    public int getTermial(int[] nums, int target,int index0){
        int index1=index0;
        while (nums[index1]<=target){
            index1+=1;
            if(index1>=nums.length){
                break;
            }
        }
        return index1-1;
    }

    public static void main(String[] args) {
        Solution s=new Solution();
        int [] result = {-1,-1} ;
        int [] nums= {2,2};
        result= s.searchRange(nums,2);
        System.out.println(result[0]);
        System.out.println(result[1]);
    }
}
